package utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * @version V1.0
 * @Title: 通用工具类
 * @Package
 * @Description:
 * @author: GaleBlade
 * @date:
 */
public class CommonUtils {

    /**
     * 从list中随机抽取元素
     *
     * @param strings
     * @param n
     * @return void
     * @throws
     * @Title: createRandomList
     * @Description: TODO
     */
    public static List<String> createRandomList(String[] strings, int n) {
        // TODO Auto-generated method stub
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, strings);
        HashMap map = new HashMap();
        List<String> listNew = new ArrayList();
        if (list.size() <= n) {
            return list;
        } else {
            while (map.size() < n) {
                int random = (int) (Math.random() * list.size());
                if (!map.containsKey(random)) {
                    map.put(random, "");
                    listNew.add(list.get(random));
                }
            }
            return listNew;
        }
    }

}
